//
//  NWProgressView.swift
//  Weibo01
//
//  Created by 牛威 on 15/12/17.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit



class NWProgressView: UIImageView {
    
    //MARK: - 进度值 0-1
    var progressNum : CGFloat = 0 {
    
        
        didSet {  //传值到进度视图中

            progress.progressNum = progressNum

        }
    
    }

    //MARK: - 构造函数 一旦给构造函数指定了参数,系统便不再提供默认的构造函数
    
    init(){
    
      super.init(frame: CGRectZero)
        
        setupUI()
    
    }
    
//    override init(frame: CGRect) {
//        super.init(frame:frame)
//        
//        setupUI()
//    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置界面
    
    private func setupUI(){
    
       addSubview(progress)
        
        //设置布局
        progress.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self.snp_edges)
        }
    
    }
    
    
    //MARK: - 懒加载控件
    
    
    private lazy var progress : ProgressView = ProgressView()
    
}


private class  ProgressView : UIView {
    
    
    var progressNum : CGFloat = 0 {
        
        didSet { //传过来之后重绘视图
            
          setNeedsDisplay()
            
        }
        
    }



    //MARK: - 画图
    
    override func drawRect(rect: CGRect) {
        
        
        /**
          arcCenter 被绘图像的圆心坐标
          radius  半径
          start 开始绘制的弧度---默认是在3点钟方向
          end   结束绘制的弧度
          closkwise  是否顺时针
         
         
         */
        
        let center = CGPoint(x: rect.width*0.5,y: rect.height*0.5)
        
        let radius = min(rect.width, rect.height)*0.5
        
        let start = CGFloat(-M_PI_2)
        
        let end = start + progressNum * CGFloat( 2 * M_PI)
    
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
        
        //向圆心画一条线 封闭
        
        path.addLineToPoint(center)
        
        path.closePath()
        
        UIColor(white: 1, alpha: 0.3).setFill()
            
        path.fill()
        
        
    }

}